//
//  BMoreContentView.swift
//  BMort_SwiftUI
//
//  Created by anscen on 2022/9/2.
//

import SwiftUI

struct BMoreContentView: View {
    public var model :BMortgage
    @State var listData :[BMoreModel] = []
    var body: some View {
        VStack.init {
            List(listData, id: \.self) { item in
                HStack.init {
                    Text(item.title).font(.system(size: 15)).foregroundColor(.appx333333())
                    Spacer()
                    Text(item.subTitle).font(.system(size: 15)).foregroundColor(.appx666666())
                }.padding(EdgeInsets.init(top: 3, leading: 0, bottom: 3, trailing: 0))
            }
            .listStyle(.insetGrouped)
            .navigationBarTitle(model.repay.rawValue)
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: BaseNaviBarBack())
        }.onAppear {
            self.loadData()
        }
    }
    func loadData(){
        model.repay == .rate ? self.computerRate(model: model) : self.computerMoney(model: model)
    }
    func computerRate(model :BMortgage){
        var total = "0"
        var totalrate = "0"
        var every = "0"
        if let gjjmoney = model.gjjmoney,let gjjrate = model.gjjrate,let month = model.month,let symoney = model.symoney,let syrates = model.syrate,let discount = model.discount{
            let syrate = moya.numeration(.multiply, discount, syrates)
            let total1 = moya.totalMoney(money: gjjmoney, month: month, rate: gjjrate)
            let total2 = moya.totalMoney(money: symoney, month: month, rate: syrate)
            total = moya.numeration(.plus, total1, total2)
            
            let totalrate1 = moya.totalRateMoney(money: gjjmoney, month: month, rate: gjjrate)
            let totalrate2 = moya.totalRateMoney(money: symoney, month: month, rate: syrate)
            totalrate = moya.numeration(.plus, totalrate1, totalrate2)
            
            let every1 = moya.monthMoney(money: gjjmoney, month: month, rate: gjjrate)
            let every2 = moya.monthMoney(money: symoney, month: month, rate: syrate)
            every = moya.numeration(.plus, every1, every2)
            self.listData = self.getlistData(month: month, money: symoney, rate: syrate, symoney: gjjmoney, syrate: gjjrate)
        }
        else if let gjjmoney = model.gjjmoney,let gjjrate = model.gjjrate,let month = model.month{
            total = moya.totalMoney(money: gjjmoney, month: month, rate: gjjrate)
            totalrate = moya.totalRateMoney(money: gjjmoney, month: month, rate: gjjrate)
            every = moya.monthMoney(money: gjjmoney, month: month, rate: gjjrate)
            self.listData = self.getlistData(month: month, money: gjjmoney, rate: gjjrate)
        }
        //sy
        else if let symoney = model.symoney,let syrates = model.syrate,let month = model.month,let discount = model.discount{
            let syrate = moya.numeration(.multiply, discount, syrates)
            total = moya.totalMoney(money: symoney, month: month, rate: syrate)
            totalrate = moya.totalRateMoney(money: symoney, month: month, rate: syrate)
            every = moya.monthMoney(money: symoney, month: month, rate: syrate)
            self.listData = self.getlistData(month: month, money: symoney, rate: syrate)
        }
        let item1 = BMoreModel(title: "累计支付总金额(元)", subTitle: total)
   
        let item2 = BMoreModel(title: "累计支付总利息(元)", subTitle: totalrate)

        let item3 = BMoreModel(title: "每期还款(元)", subTitle: every)


        let item5 = BMoreModel(title: "期数", subTitle: "本金/利息")
        self.listData.insert(item5, at: 0)
        if let rate = model.gjjrate{
            let item4 = BMoreModel(title: "公积金利率(%)", subTitle: rate)
            self.listData.insert(item4, at: 0)
        }
        if let rate = model.syrate,let discount = model.discount{
            let syrate = moya.numeration(.multiply, rate, discount)
            let item4 = BMoreModel(title: "商业贷款利率(%)", subTitle: syrate)
            self.listData.insert(item4, at: 0)
        }
        self.listData.insert(item3, at: 0)
        self.listData.insert(item2, at: 0)
        self.listData.insert(item1, at: 0)


    
    }
    func getlistData(month :String,money :String,rate :String,symoney :String? = nil,syrate :String? = nil) -> [BMoreModel]{
        var listData :[BMoreModel] = []
        if let count = Int(month){
            for index in 1...count {
                var rates = moya.rateMoney(money: money, month: month, rate: rate, index: index)
                var rep = moya.capMonty(money: money, month: month, rate: rate, index: index)
                if let symoney = symoney,let syrate = syrate {
                    let sy = moya.rateMoney(money: symoney, month: month, rate: syrate, index: index)
                    rates = moya.numeration(.plus, rates, sy)
                    let reps = moya.capMonty(money: symoney, month: month, rate: syrate, index: index)
                    rep = moya.numeration(.plus, rep, reps)
                }
                let item5 = BMoreModel(title: "第\(index)期", subTitle: "\(rep)/\(rates)")
                listData.append(item5)
            }
        }
        return listData
    }
    func computerMoney(model :BMortgage){
        var total = "0"
        var totalrate = "0"
        var every = "0"
        if let gjjmoney = model.gjjmoney,let gjjrate = model.gjjrate,let month = model.month,let symoney = model.symoney,let syrates = model.syrate,let discount = model.discount{
            let syrate = moya.numeration(.multiply, discount, syrates)
            let total1 = moya.totalmoney(money: gjjmoney, month: month, rate: gjjrate)
            let total2 = moya.totalmoney(money: symoney, month: month, rate: syrate)
            total = moya.numeration(.plus, total1, total2)
            
            let totalrate1 = moya.totalrate(money: gjjmoney, month: month, rate: gjjrate)
            let totalrate2 = moya.totalrate(money: symoney, month: month, rate: syrate)
            totalrate = moya.numeration(.plus, totalrate1, totalrate2)
            
            let every1 = moya.everymoney(money: gjjmoney, month: month)
            let every2 = moya.everymoney(money: symoney, month: month)
            every = moya.numeration(.plus, every1, every2)
            self.listData = self.getlistDataMoney(month: month, money: symoney, rate: syrate, symoney: gjjmoney, syrate: gjjrate)
        }
        else if let gjjmoney = model.gjjmoney,let gjjrate = model.gjjrate,let month = model.month{
            total = moya.totalmoney(money: gjjmoney, month: month, rate: gjjrate)
            totalrate = moya.totalrate(money: gjjmoney, month: month, rate: gjjrate)
            every =  moya.everymoney(money: gjjmoney, month: month)
            self.listData = self.getlistDataMoney(month: month, money: gjjmoney, rate: gjjrate)
        }
        //sy
        else if let symoney = model.symoney,let syrates = model.syrate,let month = model.month,let discount = model.discount{
            let syrate = moya.numeration(.multiply, discount, syrates)
            total = moya.totalmoney(money: symoney, month: month, rate: syrate)
            totalrate = moya.totalrate(money: symoney, month: month, rate: syrate)
            every = moya.everymoney(money: symoney, month: month)
            self.listData = self.getlistDataMoney(month: month, money: symoney, rate: syrate)
        }
        let item1 = BMoreModel(title: "累计支付总金额(元)", subTitle: total)
        let item2 = BMoreModel(title: "累计支付总利息(元)", subTitle: totalrate)
        let item3 = BMoreModel(title: "每期本金(元)", subTitle: every)

        let item4 = BMoreModel(title: "期数", subTitle: "还款金额/利息(元)")
        self.listData.insert(item4, at: 0)
        if let rate = model.gjjrate{
            let item5 = BMoreModel(title: "公积金利率(%)", subTitle: rate)
            self.listData.insert(item5, at: 0)
        }
        if let rate = model.syrate,let discount = model.discount{
            let syrate = moya.numeration(.multiply, rate, discount)
            let item5 = BMoreModel(title: "商业贷款利率(%)", subTitle: syrate)
            self.listData.insert(item5, at: 0)
        }
        self.listData.insert(item3, at: 0)
        self.listData.insert(item2, at: 0)
        self.listData.insert(item1, at: 0)
    }
    func getlistDataMoney(month :String,money :String,rate :String,symoney :String? = nil,syrate :String? = nil) -> [BMoreModel]{
        var listData :[BMoreModel] = []
        if let count = Int(month){
            for index in 1...count {
                var rates = moya.everyMoneyRate(money: money, month: month, rate: rate,index: index)
                var rep = moya.everyrate(money: money, month: month, rate: rate, index: index)
                if let symoney = symoney,let syrate = syrate {
                    let sy = moya.everyMoneyRate(money: symoney, month: month, rate: syrate, index: index)
                    rates = moya.numeration(.plus, rates, sy)
                    let reps = moya.everyrate(money: symoney, month: month, rate: syrate, index: index)
                    rep = moya.numeration(.plus, rep, reps)
                }
                let item5 = BMoreModel(title: "第\(index)期", subTitle: "\(rates)/\(rep)")
                listData.append(item5)
            }
        }
        return listData
    }
}

